package org.LZQ.ShortLink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.LZQ.ShortLink.admin.common.convention.result.Result;
import org.LZQ.ShortLink.admin.common.convention.result.Results;
import org.LZQ.ShortLink.admin.dto.req.UserLoginReqDTO;
import org.LZQ.ShortLink.admin.dto.req.UserRegisterReqDTO;
import org.LZQ.ShortLink.admin.dto.req.UserUpdateReqDTO;
import org.LZQ.ShortLink.admin.dto.resp.UserActualRespDTO;
import org.LZQ.ShortLink.admin.dto.resp.UserLoginRespDTO;
import org.LZQ.ShortLink.admin.dto.resp.UserRespDTO;
import org.LZQ.ShortLink.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/shortLink/admin/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        return Results.success(userService.getUserByUsername(username));
    }
    /**
     * 根据用户名查询无脱敏用户信息
     */
    @GetMapping("/actual/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 注册用户
     * @param requestParam
     * @return
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.registerUser(requestParam);
        return Results.successWithmessage("注册成功");
    }

    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.loginUser(requestParam), "登录成功");
    }

    @GetMapping("/check-login")
    public Result<Boolean> checkLogin(@RequestParam ("username") String username,@RequestParam("token") String token) {
        boolean result = userService.checkLogin(username, token);
        return Results.success(result,result?"用户已登录":"用户未登录");
    }
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.successWithmessage("退出成功");
    }
    /**
     * 更新用户信息
     * @param requestParam
     * @return
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.updateUser(requestParam);
        return Results.successWithmessage("用户信息更新成功");
    }


}
